package com.ruoyi.lock.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lock.domain.ACheckaIn;
import com.ruoyi.lock.mapper.ACheckaInMapper;
import com.ruoyi.lock.service.IACheckaInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 打卡Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class ACheckaInServiceImpl implements IACheckaInService 
{
    @Autowired
    private ACheckaInMapper aCheckaInMapper;
//    @Autowired
//    private IAKeyService aKeyService;

    /**
     * 查询打卡
     * 
     * @param id 打卡主键
     * @return 打卡
     */
    @Override
    public ACheckaIn selectACheckaInById(Long id)
    {
        return aCheckaInMapper.selectACheckaInById(id);
    }

    /**
     * 查询打卡列表
     * 
     * @param aCheckaIn 打卡
     * @return 打卡
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ACheckaIn> selectACheckaInList(ACheckaIn aCheckaIn)
    {
        return aCheckaInMapper.selectACheckaInList(aCheckaIn);
    }

    /**
     * 新增打卡
     * 
     * @param aCheckaIn 打卡
     * @return 结果
     */
    @Override
    public int insertACheckaIn(ACheckaIn aCheckaIn)
    {

        aCheckaIn.setCreateTime(DateUtils.getNowDate());
        aCheckaIn.setCheckinDate(DateUtils.getNowDate());
        int i = aCheckaInMapper.insertACheckaIn(aCheckaIn);
//        if(i > 0){
//            //打卡成功后解冻
//            AKey aKey1 = new AKey();
//            aKey1.setUniCode(aCheckaIn.getUniCode());
//            aKeyService.jiedongNEW(aKey1);
//        }

        return i;
    }

    /**
     * 修改打卡
     * 
     * @param aCheckaIn 打卡
     * @return 结果
     */
    @Override
    public int updateACheckaIn(ACheckaIn aCheckaIn)
    {
        aCheckaIn.setUpdateTime(DateUtils.getNowDate());
        return aCheckaInMapper.updateACheckaIn(aCheckaIn);
    }

    /**
     * 批量删除打卡
     * 
     * @param ids 需要删除的打卡主键
     * @return 结果
     */
    @Override
    public int deleteACheckaInByIds(Long[] ids)
    {
        return aCheckaInMapper.deleteACheckaInByIds(ids);
    }

    /**
     * 删除打卡信息
     * 
     * @param id 打卡主键
     * @return 结果
     */
    @Override
    public int deleteACheckaInById(Long id)
    {
        return aCheckaInMapper.deleteACheckaInById(id);
    }


    @Override
    public List<ACheckaIn> selectCheckaInDirection(String phone) {
        return aCheckaInMapper.selectCheckaInDirection(phone);
    }

    @Override
    public List<ACheckaIn> checkAInList(ACheckaIn aCheckaIn) {
        return aCheckaInMapper.checkAInList(aCheckaIn);
    }

    @Override
    public List<JSONObject> checkAInListByDate(ACheckaIn aCheckaIn) {
        String phone = SecurityUtils.getLoginUser().getUser().getPhonenumber();
        aCheckaIn.setPhone(phone);
        return aCheckaInMapper.checkAInListByDate(aCheckaIn);
    }

    @Override
    public List<JSONObject> checkAInListByDate11(ACheckaIn aCheckaIn) {
        return aCheckaInMapper.checkAInListByDate11(aCheckaIn);
    }

    /**
     * 获取用户某月的打卡日期列表
     *
     * @param tenantId 用户ID
     * @param homeId 住宅ID
     * @param yearMonth 年月（格式：yyyy-MM）
     * @return 打卡日期列表
     */
    @Override
    public List<Map<String, Object>> getCheckinDatesByMonth(Long tenantId, String homeId, String yearMonth) {
        return aCheckaInMapper.selectCheckinDatesByMonth(tenantId, homeId, yearMonth);
    }
}
